﻿using System.Linq;
using System.Web.Mvc;
using OnlinePharmacyV001.Models;
using System.Data;
using System.Collections.Generic;
using System;

namespace OnlinePharmacyV001.Controllers
{
    public class CategoryController : Controller
    {
        OnlinePharmacyEntities db = new OnlinePharmacyEntities();
        //
        // GET: /Category/

        public ActionResult Index()
        {

            if (ModelState.IsValid)
                return View(db.Categories.ToList());
            else return View(new List<Category>());
        }

        //
        // GET: /Category/Details/5

        public ActionResult Details(int id)
        {
            Category c = db.Categories.Find(id);
            return View(c);
        }

        //
        // GET: /Category/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Category/Create

        [HttpPost]
        public ActionResult Create(Category c)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.Categories.Add(c);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }

                return View(c);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //
        // GET: /Category/Edit/5

        public ActionResult Edit(int id)
        {
            Category c = db.Categories.Find(id);
            if (c == null)
            {
                return HttpNotFound();
            }
            return View(c);
        }

        //
        // POST: /Category/Edit/5

        [HttpPost]
        public ActionResult Edit(Category c)
        {
            if (ModelState.IsValid)
            {
                var r = Request;
                db.Entry(c).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(c);
        }

        //
        // GET: /Category/Delete/5

        public ActionResult Delete(int id)
        {
            Category c = db.Categories.Find(id);
            return View(c);
        }

        //
        // POST: /Category/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            try
            {
                // TODO: Add delete logic here
                Category c = db.Categories.Find(id);
                db.Categories.Remove(c);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
